package org.testis.gui.mvc.controller.dynamicmap;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.controller.DynamicMapControllerList;
import org.testis.gui.mvc.model.dynamicmap.WebPageParserModel;
import org.testis.gui.mvc.utilities.PageXmlParser;
import org.testis.gui.mvc.view.dynamicmap.WebPageParserView;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import org.openqa.selenium.server.SeleniumServer;


/**
 *
 * @author nurhat
 */
public class WebPageParserController extends AbstractController
{

    public static final String SHOW_WEB_PAGE_PARSER =
        "WebPageParserVisibility";
    public static final String CHANGE_BROWSER_TYPE = "BrowserType";
    public static final String CHANGE_DECIVE_ADDRESS = "DeviceAddress";
    public static final String CHANGE_OBJECT_TYPE = "ObjectType";
    public static final String CHANGE_SELECTED_FRAME = "SelectedFrame";
    public static final String CHANGE_NAVIGATING = "Navigating";
    public static final String CHANGE_SELECTED_OBJECTS = "SelectedObjects";
    public static final String PARSE_SELECTED_OBJECTS = "ParsedObjects";
    public static final String SHOW_ERROR = "LastError";
    private SeleniumServer m_server;
    private Selenium m_sel;


    public WebPageParserController()
    {
        super();
    }


    public void operationNewWebPageParser()
    {
        initialize(false);
        setModelProperty(SHOW_WEB_PAGE_PARSER,
                         true);
    }


    public void initialize(boolean editMode)
    {
        WebPageParserModel webPageParserModel = new WebPageParserModel();
        WebPageParserView webPageParserView = new WebPageParserView(
            DynamicMapControllerList.newInstance().getParentFrame(),
            true,
            editMode);
        super.addModel(webPageParserModel);
        super.addView(webPageParserView);
        webPageParserView.pack();
    }


    public void operationChangeBrowserType(String browserType)
    {
        setModelProperty(CHANGE_BROWSER_TYPE,
                         browserType);
    }


    public void operationChangeDeviceAddress(String deviceAddress)
    {
        setModelProperty(CHANGE_DECIVE_ADDRESS,
                         deviceAddress);
    }


    public void operationChangeObjectType(String objectType)
    {
        setModelProperty(CHANGE_OBJECT_TYPE,
                         objectType);
    }


    public void operationParseObjects()
    {
        try {
            PageXmlParser parser = new PageXmlParser();
            setModelProperty(PARSE_SELECTED_OBJECTS,
                             parser.parseXmlString(getModelProperty(
                                                   CHANGE_SELECTED_OBJECTS).
                                                   toString()));
        } catch (Exception ex) {
            setModelProperty(SHOW_ERROR,
                             ex.toString());
        }
    }


    public void operationChangeSelectedObjects()
    {
        try {

            String output = m_sel.getEval("selenium.browserbot.getAll" +
                                          getModelProperty(CHANGE_OBJECT_TYPE).
                                          toString() +
                                          "();");

            setModelProperty(CHANGE_SELECTED_OBJECTS,
                             output);
        } catch (Exception ex) {
            setModelProperty(SHOW_ERROR,
                             ex.toString());
        }
    }


    public void operationChangeSelectedFrame(String selectedFrame)
    {

        try {
            m_sel.selectFrame("relative=top");
            m_sel.selectFrame(selectedFrame);
            setModelProperty(CHANGE_SELECTED_FRAME,
                             selectedFrame);
        } catch (Exception ex) {
            setModelProperty(SHOW_ERROR,
                             ex.toString());
        }

    }


    public void operationChangeNavigating(boolean navigating)
    {
        if (navigating) {
            startSelenium();
        }
        else {
            stopSelenium();
        }
    }


    public void operationCloseWindow()
    {
        try {
            if (m_sel != null) {
                m_sel.stop();
            }
            if (m_server != null) {
                m_server.stop();
            }
        } catch (Exception ex) {
        }
        this.cleanAll();
    }


    private void startSelenium()
    {
        try {
            m_server = new SeleniumServer();
            m_server.start();
            m_sel = new DefaultSelenium("localhost",
                                        m_server.getPort(),
                                        "*" + getModelProperty(CHANGE_BROWSER_TYPE).
                                        toString(),
                                        getModelProperty(CHANGE_DECIVE_ADDRESS).
                                        toString());
            m_sel.start();
            m_sel.waitForCondition(
                "var popupWindow = window.open('" + getModelProperty(
                CHANGE_DECIVE_ADDRESS).toString() +
                "', 'popupWindow');selenium.browserbot.openedWindows['popupWindow'] = popupWindow;",
                "60000");
            m_sel.waitForPopUp("popupWindow",
                               "4000");
            m_sel.selectWindow("popupWindow");
            /*  sel.waitForPageToLoad("5000");*/

            setModelProperty(CHANGE_NAVIGATING,
                             true);
        } catch (Exception ex) {
            setModelProperty(SHOW_ERROR,
                             ex.toString());
            try {
                m_server.stop();
                m_sel.stop();
            } catch (Exception ex2) {
                setModelProperty(SHOW_ERROR,
                                 ex2.toString());
            }
        }
    }


    private void stopSelenium()
    {
        try {
            if (m_sel != null) {
                m_sel.stop();
            }


            if (m_server != null) {
                m_server.stop();
            }


            setModelProperty(CHANGE_NAVIGATING,
                             false);
        } catch (Exception ex) {
            setModelProperty(SHOW_ERROR,
                             ex.toString());
        }
    }
}
